/*
 * 
 * Copyright 2010 David H Pollack
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 * 
 * 
 */

package com.dhpollack.rotogss.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheFactory;
import net.sf.jsr107cache.CacheManager;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

/**
 * @author davidpl
 *
 */
public class CronTestServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    throws ServletException, IOException {
		StringBuilder s = new StringBuilder();
		
		Random r = new Random();
		Integer passcode = r.nextInt();
		StringBuilder y_url = new StringBuilder();
		y_url.append("http://rotogss.appspot.com/fantasyapi");
		y_url.append("?user=");
		y_url.append("DHPollack");
		y_url.append("&url=");
		y_url.append("http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=mlb/leagues;out=settings,standings");
		y_url.append("&passcode=");
		y_url.append(passcode.toString());

		Cache cache;
		
        Map props = new HashMap();
        props.put(GCacheFactory.EXPIRATION_DELTA, 30);
		
        try {
            CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
            cache = cacheFactory.createCache(props);
            
            cache.put(passcode.toString(),"DHPollack");
            
        } catch (CacheException e) {
            // ...
        }
        

		
		try {
            URL url = new URL(y_url.toString());
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                // ...
            	log("Line: " + line);
            	s.append(line);
            }
            reader.close();

        } catch (MalformedURLException e) {
            // ...
        	log("MalformedUrl");
        }
        
        resp.setContentType("text/xml");
        resp.getWriter().print(s.toString());
        
	}


}
